package bupt.com.test.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;
import android.util.Log;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * Created by Wyf on 2016/8/1.
 * 获取硬件信息
 */
public class HardwareUtils {
    /**
     * 获取硬件型号
     */
    public static String getPhoneInfo(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
//        String mtyb = android.os.Build.BRAND;// 手机品牌
        String mtype = android.os.Build.MODEL; // 手机型号
//        String imei = tm.getDeviceId();
//        String imsi = tm.getSubscriberId();
//        String numer = tm.getLine1Number(); // 手机号码
//        String serviceName = tm.getSimOperatorName(); // 运营商
        return mtype;
    }

    /**
     * 获取手机内存大小
     *
     * @return
     */
    public static String getTotalMemory(Context context) {
        String str1 = "/proc/meminfo";// 系统内存信息文件
        String str2;
        String[] arrayOfString;
        long initial_memory = 0;
        try {
            FileReader localFileReader = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
            str2 = localBufferedReader.readLine();// 读取meminfo第一行，系统总内存大小

            arrayOfString = str2.split("\\s+");
            for (String num : arrayOfString) {
                Log.i(str2, num + "\t");
            }

            initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存，单位是KB，乘以1024转换为Byte
            localBufferedReader.close();

        } catch (IOException e) {
        }
        return Formatter.formatFileSize(context, initial_memory);// Byte转换为KB或者MB，内存大小规格化
    }

    /**
     * 将ip的整数形式转换成ip形式
     */
    private static String int2ip(int ipInt) {
        StringBuilder sb = new StringBuilder();
        sb.append(ipInt & 0xFF).append(".");
        sb.append((ipInt >> 8) & 0xFF).append(".");
        sb.append((ipInt >> 16) & 0xFF).append(".");
        sb.append((ipInt >> 24) & 0xFF);
        return sb.toString();
    }

    /**
     * 获取Android设备IP
     */
    public static String getAndroidIp(Context context, int type) {
        String IP = null;
        if (type == ConnectivityManager.TYPE_WIFI) {
            try {
                WifiManager wifiManager = (WifiManager) context
                        .getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                int i = wifiInfo.getIpAddress();
                System.out.println("dddd=======" + i);
                IP = int2ip(i);
            } catch (Exception ex) {

            }
        }
        if (type == ConnectivityManager.TYPE_MOBILE) {
            StringBuilder IPStringBuilder = new StringBuilder();
            try {
                Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface
                        .getNetworkInterfaces();
                while (networkInterfaceEnumeration.hasMoreElements()) {
                    NetworkInterface networkInterface = networkInterfaceEnumeration
                            .nextElement();
                    Enumeration<InetAddress> inetAddressEnumeration = networkInterface
                            .getInetAddresses();
                    while (inetAddressEnumeration.hasMoreElements()) {
                        InetAddress inetAddress = inetAddressEnumeration
                                .nextElement();
                        if (!inetAddress.isLoopbackAddress()
                                && !inetAddress.isLinkLocalAddress()
                                && inetAddress.isSiteLocalAddress()) {
                            IPStringBuilder.append(inetAddress.getHostAddress()
                                    .toString() + "**");
                        }
                    }
                }
            } catch (SocketException ex) {

            }
            IP = IPStringBuilder.toString();
        }
        return IP;
    }

    /**
     * 获取版本名（Android#系统版本号#应用版本号：Android#4.1.1#1.0）
     */
    public static String getAndroidCode() {
         return "Android#" + android.os.Build.VERSION.RELEASE ;
    }
}
